//
//  XHRMedicalRecordItem.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/6.
//
//

#import "XHRMedicalRecordItem.h"
#import <objc/runtime.h>
#import <objc/message.h>
@interface XHRMedicalRecordItem()<NSCoding>


@end

@implementation XHRMedicalRecordItem
{
    CGFloat _cellHeight;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int outCount = 0;
    Ivar *vars = class_copyIvarList([self class], &outCount);
    for (int i = 0; i < outCount; i ++) {
        Ivar var = vars[i];
        const char *name = ivar_getName(var);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        unsigned int outCount = 0;
        Ivar *vars = class_copyIvarList([self class], &outCount);
        for (int i = 0; i < outCount; i ++) {
            Ivar var = vars[i];
            const char *name = ivar_getName(var);
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
    }
    return self;
}
- (CGFloat)cellHeight
{
    if (!_cellHeight) {
        _cellHeight = 0.0;
        //计算时间labe的高度
        CGFloat timeLabelHeight = [self.time sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}].height;
        _cellHeight += XHRMedicalRecordMargin + timeLabelHeight + XHRMedicalRecordMargin;
        
        CGFloat contentHeight = 0.0;
        //计算疾病类型和基本类型label的高度
        CGFloat illTypeLabelHeight = [@"疾病类型:" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}].height;
        
        contentHeight +=  XHRMedicalRecordMargin + illTypeLabelHeight * 2;
                if (self.image)
        {
            //计算正文
            CGSize maxSize = CGSizeMake(XHRScreenWidth - XHRMedicalRecordMargin - 48.5 - 80 - XHRMedicalRecordMargin, MAXFLOAT);
            CGFloat realTextHeight = [self.detailDescription boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
            
            contentHeight += XHRMedicalRecordMargin + realTextHeight;

            contentHeight >= 80?(_cellHeight += contentHeight):(_cellHeight += 80);
        }
        else
        {
            //计算正文
            CGSize maxSize = CGSizeMake(XHRScreenWidth - XHRMedicalRecordMargin - 48.5, MAXFLOAT);
            CGFloat realTextHeight = [self.detailDescription boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size.height;
            
            contentHeight += XHRMedicalRecordMargin + realTextHeight;

            _cellHeight += contentHeight;
        }
        _cellHeight += XHRMedicalRecordMargin;
    }
    return _cellHeight;
}

@end
